
var sbglResArray = [];
var ckglResArray = [];
var rkglResArray = [];

///////////////////////////////////////////////设备管理///////////////////////////////////////////
function initYjdcSbgl_table(){
	$("#yjdcSbgl_table").datagrid({
			url:"",
			striped : true,
			pageSize : 10,
			height : 315,
			nowrap : false,
			columns : [[ {
						field : '1_areaName',
						title : '资源所在区域',
						width : 120,
						align : 'center',
						editor : 'text'
					},{
						field : '0_yjzy01020',
						title : '资源所属单位',
						width : 160,
						align : 'center',
						editor : 'text'
					},{
						field : '0_yjzy01030',
						title : '资源类型',
						width : 80,
						align : 'center',
						editor : 'text',
						formatter : function(value, rec, rowIndex) {// value id rec
							// 该对象
							//curRecObj = rec;
							var lx = "";
							if(rec[0].yjzy01030==1){
								lx="人员";
							}
							if(rec[0].yjzy01030==2){
								lx="车辆";
							}
							if(rec[0].yjzy01030==3){
								lx="设备";
							}
							if(rec[0].yjzy01030==4){
								lx="卫星资源";
							}
							var resutsHTML = "";
							var resutsHTML = "<div style='align:center;'>"
									+ lx
									+"</div>";
							return resutsHTML;
					}
					},{
						field : '0_yjzy01040',
						title : '资源名称',
						width : 80,
						align : 'center',
						editor : 'text'
					},{
						field : '0_yjzy01060',
						title : '设备状态',
						width : 80,
						align : 'center',
						editor : 'text',
						formatter : function(value, rec, rowIndex) {// value id rec
						var objArr = top.infoArr;
							var zt = "";
							if(rec[0].yjzy01060==0){
								zt="损坏";
							}
							if(rec[0].yjzy01060==1){
								zt="正常";
							}
							var resutsHTML = "";
							var resutsHTML = "<div style='align:center;'>"
									+ zt
									+"</div>";
							return resutsHTML;
						}
					},{
						field : '0_yjzy01090',
						title : '资源使用状态',
						width : 80,
						align : 'center',
						editor : 'text',
						formatter : function(value, rec, rowIndex) {// value id rec
							var zt = "";
							if(rec[0].yjzy01090==0){
								zt="未使用";
							}
							if(rec[0].yjzy01090==1){
								zt="使用中";
							}
							var resutsHTML = "";
							var resutsHTML = "<div style='align:center;'>"
									+ zt
									+"</div>";
							return resutsHTML;
						}
					},{
						field : 'oper',
						title : '操作',
						width : 140,
						align : 'center',
						formatter : function(value, rec, rowIndex) { 
							sbglResArray.push(rec);
							var num = sbglResArray.length-1;
							var resutsHTML = "";
							var resutsHTML = "<div style='align:center;'>"
									+ "<a name='showDetails' style='padding:5px;cursor:pointer;text-decoration: underline;' onclick='showDetailsSB(\""
									+ num
									+ "\",false)'>详情</a>"
									+ "<a name='showDetails' style='padding:5px;cursor:pointer;text-decoration: underline;' onclick='showDetailsSB(\""
									+ num
									+ "\",true)'>编辑</a>"
									+ "<a name='showDetails' style='padding:5px;cursor:pointer;text-decoration: underline;' onclick='deleteSB(\""
									+ num
									+ "\",false)'>删除</a>"
									+"</div>";
							return resutsHTML;
						}
					}]],
			pagination : true,
			rownumbers : true,
			singleSelect : true,
		});
}

//查询设备
function querySbgl(){
	var sheng = $("#yjdcSbglShen").val();
	var shi = $("#yjdcSbglShi").val();
	var xian = $("#yjdcSbglXian").val();
	var areaCode = handlerAreaFunc(shen,shi,xian);
	areaCode = handlerZhCodeFunc(areaCode).substring(0,6);
	var zylx = $("#yjdcSbglZYLX").val();
	var zyzt = $("#yjdcSbglSBZT").val();
	var data = {
		"areaCode":areaCode,
		"zylx":zylx,
		"zyzt":zyzt,
	}
	var url ="querySbglForSbgl.shtml";
	sbglResArray = [];
	$("#yjdcSbgl_table").datagrid({
		url : url,
		queryParams : data,
		onLoadError : function(res) {
			alert("查询失败!");
		},
		onLoadSuccess : function(res) {
		}
	});
}

//添加设备
function AddYjdcSB(){
	$("#YjdcSbglAddDlg").dialog("open");
}

//添加设备
function AddYjdcSBForSbgl(){
	if($("#addyjzy01020").val()==""||$("#addyjzy01030").val()==""||$("#addyjzy01050").val()==""){
		alert("必须填写完整单位、类型和编号。");
		return;
	}
	var yjzy01010 = '420000';
	var shi = $("#yjdcSbglAddShi").val();
	var xian = $("#yjdcSbglAddXian").val();
	if(shi!=""){
		yjzy01010 = shi;
	}
	if(xian!=""){
		yjzy01010 = xian;
	}
	var dataObj = {
		"yjzy01010":yjzy01010,
		"yjzy01020":$("#addyjzy01020").val(),
		"yjzy01030":$("#addyjzy01030").val(),
		"yjzy01040":$("#addyjzy01040").val(),
		"yjzy01050":$("#addyjzy01050").val(),
		"yjzy01060":$("#addyjzy01060").val(),
		"yjzy01070":$("#addyjzy01070").val(),
		"yjzy01080":$("#addyjzy01080").val(),
		"yjzy01090":$("#addyjzy01090").val(),
		"yjzy01140":$("#addyjzy01140").val()
	}
	var data = JSON.stringify(dataObj);
	var url = "AddYjdcSBForSbgl.shtml";
	$.post(url,{"yjzy01":data},AddYjdcSBForSbglResult);
}

function AddYjdcSBForSbglResult(res){
	if(res.length>4){
	 	//内容的转化（内容有标签时），防止谷歌和火狐抽风之举
		var hiddenDiv = $("<div>");
		hiddenDiv.html(res);
		res = hiddenDiv.text();
	}
	if(res=="true"){
		alert("添加成功！");
		$("#YjdcSbglAddDlg").dialog("close");
		$("#yjdcSbgl_table").datagrid("reload");
	}else{
		alert("添加失败！");
	}
}

//展示设备详情
function showDetailsSB(num,flag){
	var readOnly = "readOnly";
	var disabled= "disabled";
	if(flag){
		readOnly = "";
		disabled= "";;
		$("#btnForSbgl").attr("style","display:display");
	}else{
		$("#btnForSbgl").attr("style","display:none");
	}
	var obj = sbglResArray[num];
	updateSBForSbgl.id = obj[0].yjzy01000;
	updateSBForSbgl.yjzy01010 = obj[0].yjzy01010;
	$("#yjzy01010").attr("value",obj[1].areaName).attr("readOnly",readOnly).attr("disabled","disabled");
	$("#yjzy01020").attr("value",obj[0].yjzy01020).attr("readOnly",readOnly).attr("disabled",disabled);
	$("#yjzy01030").attr("value",obj[0].yjzy01030).attr("readOnly",readOnly).attr("disabled",disabled);
	$("#yjzy01040").attr("value",obj[0].yjzy01040).attr("readOnly",readOnly).attr("disabled",disabled);
	$("#yjzy01050").attr("value",obj[0].yjzy01050).attr("readOnly",readOnly).attr("disabled",disabled);
	$("#yjzy01060").attr("value",obj[0].yjzy01060).attr("readOnly",readOnly).attr("disabled","disabled");
	$("#yjzy01070").attr("value",obj[0].yjzy01070).attr("readOnly",readOnly).attr("disabled",disabled);
	$("#yjzy01080").attr("value",obj[0].yjzy01080).attr("readOnly",readOnly).attr("disabled",disabled);
	$("#yjzy01090").attr("value",obj[0].yjzy01090).attr("readOnly",readOnly).attr("disabled",disabled);
	$("#yjzy01140").attr("value",obj[0].yjzy01140).attr("readOnly",readOnly).attr("disabled",disabled);
	$("#YjdcSbglXiangqingDlg").dialog("open");
}

//删除设备信息
function deleteSB(num){
	var obj = sbglResArray[num];
	if(confirm("确定删除？")){
		var id= obj[0].yjzy01000;
		var data = {"id":id};
		var url = "deleteSbForSbgl.shtml";
		$.post(url,data, deleteSbForSbglResult);
	}else{
		return ;
	}
}

//删除成功的回调
function deleteSbForSbglResult(res){
	if(res.length>4){
	 	//内容的转化（内容有标签时），防止谷歌和火狐抽风之举
		var hiddenDiv = $("<div>");
		hiddenDiv.html(res);
		res = hiddenDiv.text();
	}
	if(res=="true"){
		alert("删除成功！");
		$("#YjdcSbglXiangqingDlg").dialog("close");
		$("#yjdcSbgl_table").datagrid("reload");
	}else{
		alert("删除失败！");
	}
}

function updateSBForSbgl(){
	var id = updateSBForSbgl.id;
	var yjzy01010 = updateSBForSbgl.yjzy01010
	var dataObj = {
		"yjzy01000":id,
		"yjzy01010":yjzy01010,
		"yjzy01020":$("#yjzy01020").val(),
		"yjzy01030":$("#yjzy01030").val(),
		"yjzy01040":$("#yjzy01040").val(),
		"yjzy01050":$("#yjzy01050").val(),
		"yjzy01060":$("#yjzy01060").val(),
		"yjzy01070":$("#yjzy01070").val(),
		"yjzy01080":$("#yjzy01080").val(),
		"yjzy01090":$("#yjzy01090").val(),
		"yjzy01140":$("#yjzy01140").val()
	}
	var data = JSON.stringify(dataObj);
	var url = "updateSBForSbgl.shtml";
	$.post(url,{"yjzy01":data},updataSbForSbglResult);
}

function updataSbForSbglResult(res){
	if(res.length>4){
	 	//内容的转化（内容有标签时），防止谷歌和火狐抽风之举
		var hiddenDiv = $("<div>");
		hiddenDiv.html(res);
		res = hiddenDiv.text();
	}
	if(res=="true"){
		alert("更新成功！");
		$("#YjdcSbglXiangqingDlg").dialog("close");
		$("#yjdcSbgl_table").datagrid("reload");
	}else{
		alert("更新失败！");
	}
}

/////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////出库管理/////////////////////////////////////////////

function initYjdcCkgl_table(){
	$("#yjdcCkgl_table").datagrid({
		url:"",
		striped : true,
		pageSize : 10,
		height : 315,
		nowrap : false,
		columns : [[ {
					field : '1_areaName',
					title : '资源所在区域',
					width : 120,
					align : 'center',
					editor : 'text'
				},{
					field : '0_yjzy01020',
					title : '资源所属单位',
					width : 160,
					align : 'center',
					editor : 'text'
				},{
					field : '0_yjzy01030',
					title : '资源类型',
					width : 80,
					align : 'center',
					editor : 'text',
					formatter : function(value, rec, rowIndex) {// value id rec
						// 该对象
						//curRecObj = rec;
						var lx = "";
						if(rec[0].yjzy01030==1){
							lx="人员";
						}
						if(rec[0].yjzy01030==2){
							lx="车辆";
						}
						if(rec[0].yjzy01030==3){
							lx="设备";
						}
						if(rec[0].yjzy01030==4){
							lx="卫星资源";
						}
						var resutsHTML = "";
						var resutsHTML = "<div style='align:center;'>"
								+ lx
								+"</div>";
						return resutsHTML;
				}
				},{
					field : '0_yjzy01040',
					title : '资源名称',
					width : 80,
					align : 'center',
					editor : 'text'
				},{
					field : '0_yjzy01060',
					title : '设备状态',
					width : 80,
					align : 'center',
					editor : 'text',
					formatter : function(value, rec, rowIndex) {// value id rec
					var objArr = top.infoArr;
						var zt = "";
						if(rec[0].yjzy01060==0){
							zt="损坏";
						}
						if(rec[0].yjzy01060==1){
							zt="正常";
						}
						var resutsHTML = "";
						var resutsHTML = "<div style='align:center;'>"
								+ zt
								+"</div>";
						return resutsHTML;
					}
				},{
					field : '0_yjzy01090',
					title : '资源使用状态',
					width : 80,
					align : 'center',
					editor : 'text',
					formatter : function(value, rec, rowIndex) {// value id rec
						var zt = "";
						if(rec[0].yjzy01090==0){
							zt="未使用";
						}
						if(rec[0].yjzy01090==1){
							zt="使用中";
						}
						var resutsHTML = "";
						var resutsHTML = "<div style='align:center;'>"
								+ zt
								+"</div>";
						return resutsHTML;
					}
				},{
					field : 'oper',
					title : '操作',
					width : 140,
					align : 'center',
					formatter : function(value, rec, rowIndex) { 
						ckglResArray.push(rec);
						var num = ckglResArray.length-1;
						var resutsHTML = "";
						var resutsHTML = "<div style='align:center;'>"
								+ "<a name='showDetails' style='padding:5px;cursor:pointer;text-decoration: underline;' onclick='writeCkInfo(\""
								+ num
								+ "\",false)'>填写出库信息</a>"
								+"</div>";
						return resutsHTML;
					}
				}]],
		pagination : true,
		rownumbers : true,
		singleSelect : true,
	});
}

//出库信息填写
function writeCkInfo(num){
	resetCkInfoDlg();
	var obj = ckglResArray[num];
	var yjzy02050 = obj[0].yjzy01050;
	$("#yjzy02050").attr("value",yjzy02050);
	$("#YjdcCkglDlg").dialog("open");
}

//填写完毕，调用的函数
function AddCkInfoForCkgl(){
	var yjzy02050 = $("#yjzy02050").val();
	var yjzy02100 = $("#yjzy02100").val();
	var yjzy02110 = $("#yjzy02110").val();
	var yjzy02120 = $("#yjzy02120").val();
	var dataObj = {
		"yjzy02050":yjzy02050,
		"yjzy02100":yjzy02100,
		"yjzy02110":yjzy02110,
		"yjzy02120":yjzy02120,
	}
	var data = JSON.stringify(dataObj);
	var url = "AddCkInfoForCkgl.shtml";
	$.post(url,{"yjzy02":data},AddCkInfoForCkglResult);
}


//添加出库信息之后的回掉
function AddCkInfoForCkglResult(res){
	if(res.length>4){
	 	//内容的转化（内容有标签时），防止谷歌和火狐抽风之举
		var hiddenDiv = $("<div>");
		hiddenDiv.html(res);
		res = hiddenDiv.text();
	}
	if(res=="true"){
		alert("插入成功！");
		$("#YjdcCkglDlg").dialog("close");
		$("#yjdcCkgl_table").datagrid("reload");
	}else{
		alert("插入失败！");
	}
}

function resetCkInfoDlg(){
	var yjzy02100 = $("#yjzy02100").val("");
	var yjzy02110 = $("#yjzy02110").val("");
	var yjzy02120 = $("#yjzy02120").val("");
}

//////////////////////////////////////////////////

function querySbByState(flag){
	var sheng = $("#yjdcCkglShen").val();
	var shi = $("#yjdcCkglShi").val();
	var xian = $("#yjdcCkglXian").val();
	var zylx = $("#yjdcCkglZYLX").val();
	if(flag==1){
		sheng = $("#yjdcRkglShen").val();
		shi = $("#yjdcRkglShi").val();
		xian = $("#yjdcRkglXian").val();
		zylx = $("#yjdcRkglZYLX").val();
	}
	
	var areaCode = handlerAreaFunc(shen,shi,xian);
	areaCode = handlerZhCodeFunc(areaCode).substring(0,6);
	var data = {
		flag:flag,
		areaCode:areaCode,
		zylx:zylx
	}
	
	var url = "querySbByState.shtml";
	if(flag==0){
		ckglResArray = [];
		$("#yjdcCkgl_table").datagrid({
			url : url,
			queryParams : data,
			onLoadError : function(res) {
				alert("查询失败!");
			},
			onLoadSuccess : function(res) {
			}
		});
	}else{
		rkglResArray = [];
		$("#yjdcRkgl_table").datagrid({
			url : url,
			queryParams : data,
			onLoadError : function(res) {
				alert("查询失败!");
			},
			onLoadSuccess : function(res) {
			}
		});
	}
	
}


//////////////////////////////////////////////入库管理//////////////////////////////////////////

function initYjdcRkgl_table(){
	$("#yjdcRkgl_table").datagrid({
		url:"",
		striped : true,
		pageSize : 10,
		height : 315,
		nowrap : false,
		columns : [[ {
					field : '1_areaName',
					title : '资源所在区域',
					width : 120,
					align : 'center',
					editor : 'text'
				},{
					field : '0_yjzy01020',
					title : '资源所属单位',
					width : 160,
					align : 'center',
					editor : 'text'
				},{
					field : '0_yjzy01030',
					title : '资源类型',
					width : 80,
					align : 'center',
					editor : 'text',
					formatter : function(value, rec, rowIndex) {// value id rec
						// 该对象
						//curRecObj = rec;
						var lx = "";
						if(rec[0].yjzy01030==1){
							lx="人员";
						}
						if(rec[0].yjzy01030==2){
							lx="车辆";
						}
						if(rec[0].yjzy01030==3){
							lx="设备";
						}
						if(rec[0].yjzy01030==4){
							lx="卫星资源";
						}
						var resutsHTML = "";
						var resutsHTML = "<div style='align:center;'>"
								+ lx
								+"</div>";
						return resutsHTML;
				}
				},{
					field : '0_yjzy01040',
					title : '资源名称',
					width : 80,
					align : 'center',
					editor : 'text'
				},{
					field : '0_yjzy01060',
					title : '设备状态',
					width : 80,
					align : 'center',
					editor : 'text',
					formatter : function(value, rec, rowIndex) {// value id rec
					var objArr = top.infoArr;
						var zt = "";
						if(rec[0].yjzy01060==0){
							zt="损坏";
						}
						if(rec[0].yjzy01060==1){
							zt="正常";
						}
						var resutsHTML = "";
						var resutsHTML = "<div style='align:center;'>"
								+ zt
								+"</div>";
						return resutsHTML;
					}
				},{
					field : '0_yjzy01090',
					title : '资源使用状态',
					width : 80,
					align : 'center',
					editor : 'text',
					formatter : function(value, rec, rowIndex) {// value id rec
						var zt = "";
						if(rec[0].yjzy01060==0){
							zt="未使用";
						}
						if(rec[0].yjzy01060==1){
							zt="使用中";
						}
						var resutsHTML = "";
						var resutsHTML = "<div style='align:center;'>"
								+ zt
								+"</div>";
						return resutsHTML;
					}
				},{
					field : 'oper',
					title : '操作',
					width : 140,
					align : 'center',
					formatter : function(value, rec, rowIndex) { 
						rkglResArray.push(rec);
						var num = rkglResArray.length-1;
						var resutsHTML = "";
						var resutsHTML = "<div style='align:center;'>"
								+ "<a name='showDetails' style='padding:5px;cursor:pointer;text-decoration: underline;' onclick='writeRkInfo(\""
								+ num
								+ "\",false)'>填写入库信息</a>"
								+"</div>";
						return resutsHTML;
					}
				}]],
		pagination : true,
		rownumbers : true,
		singleSelect : true,
	});
}

//点击入库信息填写，拿到资源的编号，去状态表之中查询出库的信息
function writeRkInfo(num){
	var obj  = rkglResArray[num];
	var yjzy02050 = obj[0].yjzy01050;
	var url = "queryCkInfoForRkgl.shtml";
	$.post(url,{"yjzy02050":yjzy02050},queryCkInfoForRkglResult);
}

//查询的回调
function queryCkInfoForRkglResult(res){
	var jsonObj = eval(res);
	$("#yjzy02000").attr("value",jsonObj[0].yjzy02000);
	$("#rkyjzy02050").attr("value",jsonObj[0].yjzy02050).attr("readOnly","readOnly");
	$("#rkyjzy02100").attr("value",jsonObj[0].yjzy02100).attr("readOnly","readOnly");
	$("#rkyjzy02110").attr("value",jsonObj[0].yjzy02110).attr("readOnly","readOnly");
	$("#rkyjzy02120").attr("value",jsonObj[0].yjzy02120).attr("readOnly","readOnly");
	$("#YjdcRkglDlg").dialog("open");
}

//点击添加之后调用的函数
function AddRkInfoForRkgl(){
	var dataObj = {
		"yjzy02000":$("#yjzy02000").val(),
		"yjzy02050":$("#rkyjzy02050").val(),
		"yjzy02090":$("#rkyjzy02090").val(),
		"yjzy02100":$("#rkyjzy02100").val(),
		"yjzy02110":$("#rkyjzy02110").val(),
		"yjzy02120":$("#rkyjzy02120").val(),
		"yjzy02130":$("#rkyjzy02130").val(),
		"yjzy02140":$("#rkyjzy02140").val(),
	}
	var data = JSON.stringify(dataObj);
	var url = "AddRkInfoForRkgl.shtml";
	$.post(url,{"yjzy02":data},AddRkInfoForRkglResult);
}

function AddRkInfoForRkglResult(res){
	if(res.length>4){
	 	//内容的转化（内容有标签时），防止谷歌和火狐抽风之举
		var hiddenDiv = $("<div>");
		hiddenDiv.html(res);
		res = hiddenDiv.text();
	}
	if(res=="true"){
		alert("插入成功！");
		$("#YjdcRkglDlg").dialog("close");
		$("#yjdcRkgl_table").datagrid("reload");
	}else{
		alert("插入失败！");
	}
}